<?php

/**
 * 自定义函数库
 */

/**
 * 判断是否是手机访问
 * @return boolean
 */
function is_mobile()
{
    $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
    $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
    $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
    $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
    $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
    $regex_match.=")/i";
    return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}



//获取汇率 eg:getExchangeRate('CNY','USD');
function getExchangeRate($from_Currency,$to_Currency)
{
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $url = "download.finance.yahoo.com/d/quotes.html?s=".$from_Currency.$to_Currency."=X&f=sl1d1t1ba&e=.html";
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = explode(',', $rawdata);
    return $data[1];
}


/**
 * 文件上传命名规则
 * @param string $prefix:前缀
 * @param number $len:随机数长度
 */
function getUploadName($prefix='',$len=6){

    $min	=	'';
    $max	=	'';

    for($i=0;$i<$len;$i++){
        if($i == 0){
            $min	.=	'1';
        }else{
            $min	.=	'0';
        }
        $max	.=	'9';
    }
    return $prefix.time().mt_rand(intval($min), intval($max));
}




/**
 * 直接在模板输出某个表的某个字段
 */
function getValueById($id,$table,$field){

    $val	=	"";
    if($id > 0 && $table != "" && $field != ""){
        $model		=	M($table);
        $pk			=	$model->getPk();
        $val		=	$model->where($pk.'='.$id)->getField($field);
    }
    return $val;
}

/**
 * 生成各种编号
 * @param string $type:编号类型 goods_sn商品编号  order_sn订单编号
 */
function generateSerialNumber($type,$prefix){

    $randMin	=	100000;
    $randMax	=	999999;

    switch($type){

        case "goods_sn":
            return $prefix.time().mt_rand($randMin, $randMax);
            break;

        case "order_sn":
            return $prefix.time().mt_rand($randMin, $randMax);
            break;

        default:
            return false;
            break;

    }

}

function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)  
    {  
  if(function_exists("mb_substr")){  
              if($suffix)  
              return mb_substr($str, $start, $length, $charset)."...";  
              else
                   return mb_substr($str, $start, $length, $charset);  
         }  
         elseif(function_exists('iconv_substr')) {  
             if($suffix)  
                  return iconv_substr($str,$start,$length,$charset)."...";  
             else
                  return iconv_substr($str,$start,$length,$charset);  
         }  
         $re['utf-8']   = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef]
                  [x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";  
         $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";  
         $re['gbk']    = "/[x01-x7f]|[x81-xfe][x40-xfe]/";  
         $re['big5']   = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";  
         preg_match_all($re[$charset], $str, $match);  
         $slice = join("",array_slice($match[0], $start, $length));  
         if($suffix) return $slice."…";  
         return $slice;
    }


/*正则表达式验证常规内容*/

//验证手机号
function isMobile($mobile){
    return preg_match('/^(13|15|17|18)[\d]{9}$/',$mobile);
}

//验证电子邮件
function isEmail($email){
    return filter_var($email,FILTER_VALIDATE_EMAIL) ? true : false;
}

//验证账号密码 限制6~18位
function isAccount($str){
    return preg_match('/^[a-z\d_@.]{6,18}$/i',$str);
}

//验证URL
function isURL($str){
    return filter_var($str, FILTER_VALIDATE_URL) ? true : false;
}

//验证ip
function isIP($str){
    return filter_var($str, FILTER_VALIDATE_IP) ? true : false;
}

